import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import { getDictionaryPageList, getDictionaryInfo, createDictionary, updateDictionary, deleteDictionary } from "../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>): DataAction => ({
    fetchList: (params) => {
        const { page, pageSize, filter } = params;
        return new Promise((resolve, reject) => getDictionaryPageList(Object.assign({page,pageSize}, filter || {})).then((resp) => resolve(resp.data)).catch((e) => reject(e)))
    },
    getItem: (rowKey) => {
        const dataSetId = rowKey as number;
        return new Promise((resolve, reject) => getDictionaryInfo(dataSetId).then((resp) => {
            resolve({
                dictName: resp.data.dictName,
                dictType: resp.data.dictType,
                status: resp.data.status,
                remark: resp.data.remark,
            });
        }).catch((e) => reject(e)));
    },
    createItem: (params) => {
        return new Promise((resolve, reject) => createDictionary(params).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    updateItem: (rowKey, params) => {
        return new Promise((resolve, reject) => updateDictionary(rowKey as number, params).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    deleteItems: (selectedRowKeys) => {
        return deleteDictionary(selectedRowKeys.map((item) => parseInt(item)))
    },
})


